﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Gmantis.Controls.DataGrid
{
    [TemplateVisualState(Name = "Dragging", GroupName = "MovementStates"), 
    TemplateVisualState(Name = "MouseOver", GroupName = "MovementStates"), 
    TemplateVisualState(Name = "Normal", GroupName = "MovementStates")]
    public class DataGridVerticalFreezingSeparatorPresenter : Control
    {
        // Fields
        internal bool _isLoaded;
        internal static readonly DependencyProperty ForceMouseOverProperty = DependencyProperty.Register("ForceMouseOver", typeof(bool), typeof(DataGridVerticalFreezingSeparatorPresenter), new PropertyMetadata(new PropertyChangedCallback(DataGridVerticalFreezingSeparatorPresenter.OnForceMouseOverPropertyChanged)));
        internal static readonly DependencyProperty IsDraggingProperty = DependencyProperty.Register("IsDragging", typeof(bool), typeof(DataGridVerticalFreezingSeparatorPresenter), new PropertyMetadata(false, new PropertyChangedCallback(DataGridVerticalFreezingSeparatorPresenter.OnIsDraggingPropertyChanged)));
        public static readonly DependencyProperty MouseOverBrushProperty = DependencyProperty.Register("MouseOverBrush", typeof(Brush), typeof(DataGridVerticalFreezingSeparatorPresenter), null);
        public static readonly DependencyProperty PressedBrushProperty = DependencyProperty.Register("PressedBrush", typeof(Brush), typeof(DataGridVerticalFreezingSeparatorPresenter), null);

        // Methods
        public DataGridVerticalFreezingSeparatorPresenter()
        {
            RoutedEventHandler handler = null;
            base.DefaultStyleKey = typeof(DataGridVerticalFreezingSeparatorPresenter);
            if (handler == null)
            {
                handler = (param0, param1) => this.ChangeVisualStateMovement(false);
            }
            base.Loaded += handler;
            this.SetCustomDefaultValues();
        }

        protected void ChangeVisualStateMovement(bool useTransitions)
        {
            if (this.IsDragging)
            {
                VisualStateHelper.GoToState(this, "Dragging", useTransitions);
            }
            if (this.ForceMouseOver && !this.IsDragging)
            {
                VisualStateHelper.GoToState(this, "MouseOver", useTransitions);
            }
            if (!this.ForceMouseOver && !this.IsDragging)
            {
                VisualStateHelper.GoToState(this, "Normal", useTransitions);
            }
        }

        public override void OnApplyTemplate()
        {
            string str = string.Empty;
            base.OnApplyTemplate();
            this._isLoaded = true;
            if (!string.IsNullOrEmpty(str))
            {
                this._isLoaded = false;
                if ((base.Template != null) && !PlatformIndependent.IsInDesignMode(this))
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Template cannot be applied to DataGridVerticalFreezingSeparatorPresenter.\nDetails: {0}", new object[] { str }));
                }
            }
            else
            {
                this.ChangeVisualStateMovement(false);
            }
        }

        private static void OnForceMouseOverPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as DataGridVerticalFreezingSeparatorPresenter).ChangeVisualStateMovement(true);
        }

        private static void OnIsDraggingPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as DataGridVerticalFreezingSeparatorPresenter).ChangeVisualStateMovement(true);
        }

        protected override void OnMouseEnter(MouseEventArgs e)
        {
            if (this.View != null)
            {
                this.View.Properties.IsOverVerticalFreezingSeparator = true;
            }
        }

        protected override void OnMouseLeave(MouseEventArgs e)
        {
            if (this.View != null)
            {
                this.View.Properties.IsOverVerticalFreezingSeparator = false;
            }
        }

        private void SetCustomDefaultValues()
        {
            base.IsTabStop = false;
        }

        // Properties
        internal bool ForceMouseOver
        {
            get
            {
                return (bool)base.GetValue(ForceMouseOverProperty);
            }
            set
            {
                base.SetValue(ForceMouseOverProperty, value);
            }
        }

        internal bool IsDragging
        {
            get
            {
                return (bool)base.GetValue(IsDraggingProperty);
            }
            set
            {
                base.SetValue(IsDraggingProperty, value);
            }
        }

        public Brush MouseOverBrush
        {
            get
            {
                return (Brush)base.GetValue(MouseOverBrushProperty);
            }
            set
            {
                base.SetValue(MouseOverBrushProperty, value);
            }
        }

        public Brush PressedBrush
        {
            get
            {
                return (Brush)base.GetValue(PressedBrushProperty);
            }
            set
            {
                base.SetValue(PressedBrushProperty, value);
            }
        }

        internal DataGridView View { get; set; }
    }
}
